package com.gmail.blackdog1987.onedollar.dao;

import com.gmail.blackdog1987.onedollar.dao.hibernate.EntityHibernateDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.BaseJdbcDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.JdbcUtil;
import com.gmail.blackdog1987.onedollar.entity.TPrizesEntity;
import com.gmail.blackdog1987.onedollar.entity.TUserAddressEntity;
import com.gmail.blackdog1987.onedollar.entity.base.Page;
import com.gmail.blackdog1987.onedollar.vo.LuckyBagVO;
import com.gmail.blackdog1987.onedollar.vo.PrizesVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository("luckyPanDao")
public class LuckyPanDao extends EntityHibernateDao<TPrizesEntity>{
    @Autowired
    private BaseJdbcDao baseJdbcDao;

    public Page<PrizesVO> query(String userid, int pageindex, int pagesize){
        String whereSql;
        ArrayList<Object> whereArgs = new ArrayList<Object>();
        whereSql = " from t_prizes_goods g, t_prizes_record r left join t_user u on r.user_id = u.user_id where g.id = r.prizes_id and r.status = 1 and r.user_id = ? order by r.add_time desc";

        String selectionSql = "select g.*, r.user_id, r.add_time, u.nick_name, u.head ";

        String limit = " limit " + (pageindex-1)*pagesize + "," + pagesize;
        Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" SELECT COUNT(*) " + whereSql , new String[]{userid});

        List<PrizesVO> list = JdbcUtil.queryForList(baseJdbcDao, PrizesVO.class, selectionSql + whereSql + limit, new String[]{userid});

        Page page = new Page(list, pageindex, totalCount, pagesize);

        return page;
    }

}
